package com.uvt.agenda.view;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import com.uvt.agenda.R;
import org.joda.time.DateTime;

/**
 * Created by oana  on 01/02/14.
 * 08:43
 */

public class DatePickerDialog extends Dialog {

    /**
     * View elements
     */
    private Button saveButton;
    private TextView month;
    private TextView day;
    private TextView year;
    private int
            currentYear = DateTime.now().getYear(),
            currentDay= DateTime.now().getDayOfMonth(),
            currentMonth = DateTime.now().getMonthOfYear();
    private CalendarView calendarView;
    private DialogResultListener resultListener;
    private String MONTH_FORMAT = "MMM";
    private String YEAR_FORMAT = "yyyy";
    private String DAY_FORMAT = "d";

    /**
     * Working dates
     */
    private DateTime startDate = DateTime.now();
    private DateTime endDate = DateTime.now().plusHours(1);

    public DatePickerDialog(Context context) {
        super(context, R.layout.date_picker_dialog_layout);
        initDialog();
    }

    public DatePickerDialog(Context context, DateTime startDate) {
        super(context, R.layout.date_picker_dialog_layout);
        this.startDate = startDate;
        initDialog();
    }

    private void initDialog() {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.date_picker_dialog_layout);
        this.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        month = (TextView) findViewById(R.id.month);
        day = (TextView) findViewById(R.id.day);
        year = (TextView) findViewById(R.id.year);
        saveButton = (Button) findViewById(R.id.save_btn);
        calendarView = (CalendarView) findViewById(R.id.calendar);

        calendarView.setShowWeekNumber(false);
        calendarView.setViewModeOnly(true);

        calendarView.setDate(startDate.getMillis());

        year.setText(startDate.toString(YEAR_FORMAT));
        month.setText(startDate.toString(MONTH_FORMAT));
        day.setText(startDate.toString(DAY_FORMAT));

        setClickListeners();



    }


    private void setClickListeners() {

        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int y, int m, int d) {
                // Keep aware that month is returned with one unit in minus
                int mon = m + 1;
                DateTime tmp = new DateTime().withDate(y,mon,d);
                year.setText(y+ "");
                month.setText(tmp.toString(MONTH_FORMAT));
                day.setText(d + "");
                currentDay = d;
                currentMonth = mon;
                currentYear = y;
            }
        });

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (resultListener!=null){
                    DateTime dateTime = DateTime.now().withDate(currentYear, currentMonth, currentDay);
                    resultListener.onDoneClick(dateTime);
                }
                dismiss();
            }
        });
    }

    public void setResultListener(DialogResultListener resultListener) {
        this.resultListener = resultListener;
    }
}
